#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;

void AddPeopleInfo(contacts2::PeopleInfo* people){
    string name;
    int age;
    cout << "————————————————————新增联系人————————————————————" << endl;
    
    cout << "请输入联系人姓名: ";
    getline(cin, name);
    people->set_name(name);

    cout << "请输入联系人年龄: ";
    cin >> age;
    cin.ignore(256, '\n'); // 清除输入缓冲区 -- 换行符
    people->set_age(age);


    for(int i = 0; i < 1; ++i){
        cout << "请输入联系人电话" << i + 1 << "(只输入回车完成电话新增): ";
        string number;
        getline(cin, number);
        if(number.empty()) break; // 如果输入为空，则结束电话输入
        else if(number.length() > 11){
            cout << "电话号码长度不能超过11位, 请重新输入。" << endl;
            i--;
            continue;
        }
        contacts2::PeopleInfo_Phone* phone = people->add_phone();
        phone->set_number(number);

        cout << "请输入联系人电话" << i + 1 << "的类型(1:手机, 2:座机, 3:其他): ";
        int type;
        cin >> type;
        cin.ignore(256, '\n'); // 清除输入缓冲区 -- 换行符
        switch(type){
            case 1:
                phone->set_type(contacts2::PeopleInfo_Phone_PhoneType_MP);
                break;
            case 2:
                phone->set_type(contacts2::PeopleInfo_Phone_PhoneType_TEL);
                break;
            default:
                cout << "无效的类型, 请重新输入。" << endl;
                i--;
                continue;;
        }
    }
    
    // 填写地址信息
    contacts2::Address addr;
    cout << "请输入联系人家庭地址: " ;
    string home_address;
    getline(cin, home_address);
    addr.set_home_address(home_address);

    cout << "请输入联系人的单位地址: ";
    string unit_address;
    getline(cin, unit_address);
    addr.set_unit_address(unit_address);

    // 选择联系方式
    cout << "请选择联系方式(1: QQ, 2: 微信): ";
    int op;
    cin >> op;
    cin.ignore(256, '\n'); // 清除输入缓冲区 -- 换行符
    if(1 == op){
        string qq;
        cout << "请输入QQ号: ";
        getline(cin, qq);
        people->set_qq(qq);
    }else if(2 == op){
        string wechat;
        cout << "请输入微信号: ";
        getline(cin, wechat);
        people->set_wechat(wechat);
    }else{
        cout << "无效的选项，请重新输入。" << endl;
        return;
    }

    for(int i = 0; i < 1; ++i){
        cout << "请输入联系人备注" << i + 1 << "(只输入回车完成备注新增): ";
        string remark_key;
        getline(cin, remark_key);
        if(remark_key.empty()) break; // 如果输入为空，则结束备注输入
        else if(remark_key.length() > 100){
            cout << "备注长度不能超过100位, 请重新输入。" << endl;
            i--;
            continue;
        }
        cout << "请输入备注内容: " << endl;
        string remark_content;
        getline(cin, remark_content);
        people->mutable_remark()->insert({remark_key, remark_content});
    }

    cout << "————————————————————添加联系人成功————————————————————" << endl;
}


int main(){
    GOOGLE_PROTOBUF_VERIFY_VERSION; 
    contacts2::Contacts contacts;
    // 读取本地人已存在的联系人文件
    fstream input("contacts.bin", ios::in | ios::binary);
    if(!input){
        cout << "contacts.bin not found, creating a new file." << endl;
    }else if(!contacts.ParseFromIstream(&input)){    
        cout << "Failed to parse contacts.bin." << endl;
        input.close();
        return -1;
    }
    // 向通讯录添加一个联系人
    AddPeopleInfo(contacts.add_contacts());


   // 将联系人信息写入到文件中
    fstream output("contacts.bin", ios::out | ios::trunc | ios::binary);
    if(!contacts.SerializeToOstream(&output)){
        cout << "Failed to write contacts.bin." << endl;
        output.close();
        return -1;
    }

    cout << "Write Success" << endl;
    input.close();
    output.close();
    google::protobuf::ShutdownProtobufLibrary();
    return 0;
}